﻿using System.Linq;
using ConfigExplorer.CmdClient.Extensions;
using ConfigExplorer.Model;

namespace ConfigExplorer.CmdClient.ModelTransformers
{
    class OrchestrationModelTransformer
    {
        internal static Orchestration TransformModel(Microsoft.BizTalk.ExplorerOM.BtsOrchestration omOrchestration)
        {
            var orchestration = new Orchestration();

            orchestration.Name = omOrchestration.FullName;

            orchestration.Description = omOrchestration.Description;

            foreach (Microsoft.BizTalk.ExplorerOM.OrchestrationPort omPort in omOrchestration.Ports)
            {
                orchestration.Ports.Add(OrchestrationPortModelTransformer.TransformModel(omPort));
            }

            return orchestration;
        }

        internal static void SetReferences(Orchestration orchestration, BizTalkArtifacts artifacts, Microsoft.BizTalk.ExplorerOM.BtsOrchestration omOrchestration)
        {
            orchestration.Application = artifacts.Applications[omOrchestration.Application.Id()];
            orchestration.ParentAssembly = artifacts.Assemblies[omOrchestration.BtsAssembly.Id()]; ;

            //if (omOrchestration.Host != null)
            //{
            //    orchestration.Host = new Host().Map(omOrchestration.Host);
            //}

            foreach (var port in orchestration.Ports)
            {
                OrchestrationPortModelTransformer.SetReferences(port, artifacts, omOrchestration.Ports.Cast<Microsoft.BizTalk.ExplorerOM.OrchestrationPort>().Where(o => o.Name == port.Name).SingleOrDefault());
            }
        }
    }
}
